﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SD.Infrastructure.RepositoryBase;
using SlamDunk.MES.Domain.Entities.StockContext;
using SlamDunk.MES.Domain.IRepositories.Interfaces.StockContext;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.StockContext
{
    /// <summary>
    /// 库存单据仓储实现
    /// </summary>
    public class StockOrderRepository : EFAggRootRepositoryProvider<StockOrder>, IStockOrderRepository
    {
        #region # 分页获取入库单列表 —— ICollection<StockInOrder> FindStockInOrdersByPage(string keywords...
        /// <summary>
        /// 分页获取入库单列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="stockOrderClassId">库存单据类Id</param>
        /// <param name="checkStatus">审核状态</param>
        /// <param name="sourceOrderType">源单据类型</param>
        /// <param name="sourceOrderId">源单据Id</param>
        /// <param name="targetStorageLocationType">目标储存位置类型</param>
        /// <param name="targetStorageLocationId">目标储存位置Id</param>
        /// <param name="generateBarcode">是否生成条码</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="facilityIds">工厂模型Id集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>入库单列表</returns>
        public ICollection<StockInOrder> FindStockInOrdersByPage(string keywords, Guid? stockOrderClassId, CheckStatus? checkStatus, SourceOrderType? sourceOrderType, string sourceOrderId, StorageLocationType? targetStorageLocationType, string targetStorageLocationId, bool? generateBarcode, DateTime? startTime, DateTime? endTime, IList<Guid> facilityIds, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<StockInOrder, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (stockOrderClassId == null || x.StockOrderClassId == stockOrderClassId) &&
                    (checkStatus == null || x.CheckStatus == checkStatus) &&
                    (sourceOrderType == null || x.SourceOrderType == sourceOrderType) &&
                    (string.IsNullOrEmpty(sourceOrderId) || x.SourceOrderId == sourceOrderId) &&
                    (targetStorageLocationType == null || x.TargetStorageLocationType == targetStorageLocationType) &&
                    (string.IsNullOrEmpty(targetStorageLocationId) || x.TargetStorageLocationId == targetStorageLocationId) &&
                    (generateBarcode == null || x.GenerateBarcode == generateBarcode) &&
                    (startTime == null || x.AddedTime >= startTime) &&
                    (endTime == null || x.AddedTime <= endTime);

            IQueryable<StockInOrder> stockInOrders = base.Find(condition);
            if (facilityIds != null && facilityIds.Any())
            {
                stockInOrders = stockInOrders.Where(x => x.FacilityId.HasValue && facilityIds.Contains(x.FacilityId.Value));
            }

            IOrderedQueryable<StockInOrder> orderedResult = stockInOrders.OrderByDescending(x => x.AddedTime);
            IQueryable<StockInOrder> pagedResult = orderedResult.ToPage(pageIndex, pageSize, out rowCount, out pageCount);

            return pagedResult.ToList();
        }
        #endregion

        #region # 分页获取出库单列表 —— ICollection<StockOutOrder> FindStockOutOrdersByPage(string keywords...
        /// <summary>
        /// 分页获取出库单列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="stockOrderClassId">库存单据类Id</param>
        /// <param name="checkStatus">审核状态</param>
        /// <param name="sourceOrderType">源单据类型</param>
        /// <param name="sourceOrderId">源单据Id</param>
        /// <param name="sourceStorageLocationType">源储存位置类型</param>
        /// <param name="sourceStorageLocationId">目标储存位置Id</param>
        /// <param name="targetStorageLocationType">目标储存位置类型</param>
        /// <param name="targetStorageLocationId">目标储存位置Id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="facilityIds">工厂模型Id集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>出库单列表</returns>
        public ICollection<StockOutOrder> FindStockOutOrdersByPage(string keywords, Guid? stockOrderClassId, CheckStatus? checkStatus, SourceOrderType? sourceOrderType, string sourceOrderId, StorageLocationType? sourceStorageLocationType, string sourceStorageLocationId, StorageLocationType? targetStorageLocationType, string targetStorageLocationId, DateTime? startTime, DateTime? endTime, IList<Guid> facilityIds, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<StockOutOrder, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (stockOrderClassId == null || x.StockOrderClassId == stockOrderClassId) &&
                    (checkStatus == null || x.CheckStatus == checkStatus) &&
                    (sourceOrderType == null || x.SourceOrderType == sourceOrderType) &&
                    (string.IsNullOrEmpty(sourceOrderId) || x.SourceOrderId == sourceOrderId) &&
                    (sourceStorageLocationType == null || x.SourceStorageLocationType == sourceStorageLocationType) &&
                    (string.IsNullOrEmpty(sourceStorageLocationId) || x.SourceStorageLocationId == sourceStorageLocationId) &&
                    (targetStorageLocationType == null || x.TargetStorageLocationType == targetStorageLocationType) &&
                    (string.IsNullOrEmpty(targetStorageLocationId) || x.TargetStorageLocationId == targetStorageLocationId) &&
                    (startTime == null || x.AddedTime >= startTime) &&
                    (endTime == null || x.AddedTime <= endTime);

            IQueryable<StockOutOrder> stockOutOrders = base.Find(condition);
            if (facilityIds != null && facilityIds.Any())
            {
                stockOutOrders = stockOutOrders.Where(x => x.FacilityId.HasValue && facilityIds.Contains(x.FacilityId.Value));
            }

            IOrderedQueryable<StockOutOrder> orderedResult = stockOutOrders.OrderByDescending(x => x.AddedTime);
            IQueryable<StockOutOrder> pagedResult = orderedResult.ToPage(pageIndex, pageSize, out rowCount, out pageCount);

            return pagedResult.ToList();
        }
        #endregion
    }
}
